import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:xindong/common/xindong_sezhi.dart';
import 'package:xindong/routes/userspace/xindong_kongjian_ctr.dart';
import 'package:xindong/routes/userspace/view/xindong_kongjian_app_bar.dart';
import 'package:xindong/routes/userspace/view/xindong_kongjian_header.dart';
import 'package:xindong/routes/userspace/view/xindong_kongjian_info.dart';
import 'package:xindong/routes/userspace/view/xindong_kongjian_info_auth.dart';
import 'package:xindong/routes/userspace/view/xindong_kongjian_info_basic.dart';
import 'package:xindong/routes/userspace/view/xindong_kongjian_info_gift.dart';
import 'package:xindong/routes/userspace/view/xindong_kongjian_info_moment.dart';
import 'package:xindong/routes/userspace/view/xindong_kongjian_info_noble.dart';
import 'package:xindong/routes/userspace/view/xindong_kongjian_info_photo.dart';
import 'package:xindong/routes/userspace/view/xindong_kongjian_info_self_slogan.dart';
import 'package:xindong/routes/userspace/view/xindong_kongjian_info_slogan.dart';
import 'package:xindong/routes/userspace/view/xindong_kongjian_info_tags.dart';
import 'package:xindong/routes/userspace/view/xindong_kongjian_util_bar.dart';
import 'package:xindong/widgets/common/xindong_button.dart';
import 'package:xindong/widgets/xindong_loading.dart';

class XinDongKongJianYeMian extends StatefulWidget {
  final String userId;
  final bool fromChat;

  const XinDongKongJianYeMian({super.key, required this.userId, required this.fromChat});

  @override
  State<XinDongKongJianYeMian> createState() => _XinDongKongJianYeMianState();
}

class _XinDongKongJianYeMianState extends State<XinDongKongJianYeMian> {
  // 空间控制器
  final _controller = Get.put(XinDongKongJianCtr());

  @override
  void initState() {
    super.initState();
    WidgetsBinding.instance.addPostFrameCallback((timeStamp) async {
      final result = await _controller.requestKongJian(widget.userId);
      if (result) {
        _controller.requestUserMoment(widget.userId);
        // _controller.requestConfig(widget.userId);
        _controller.requestUserVideoList(widget.userId);
        _controller.requestUserAuthList(widget.userId);
      } else {
        Future.delayed(const Duration(seconds: 1)).whenComplete(() => Get.back());
      }
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.white,
      body: GetBuilder<XinDongKongJianCtr>(
          id: XinDongKongJianCtr.updateIdLoadingInfo,
          builder: (controller) {
            if (controller.userspace == null) {
              return const XinDongLoadingCenter();
            }
            if (controller.isUserException) {
              return Center(
                child: Column(
                  mainAxisAlignment: MainAxisAlignment.center,
                  crossAxisAlignment: CrossAxisAlignment.center,
                  children: [
                    const Text("该用户信息异常，可能已经注销", style: TextStyle(fontSize: 15, color: DZColors.contentColor)),
                    const SizedBox(height: 20),
                    SizedBox(
                      width: 200,
                      height: 40,
                      child: XinDongCommonButton("返回", onTap: () => Navigator.maybePop(context)),
                    ),
                  ],
                ),
              );
            }
            return Stack(
              children: [
                MediaQuery.removePadding(
                  removeTop: true,
                  removeBottom: true,
                  context: context,
                  child: ListView(
                      controller: controller.scrollController,
                      physics: const ClampingScrollPhysics(),
                      padding: EdgeInsets.only(bottom: controller.utilBarHeight),
                      children: <Widget>[
                        XinDongKongJianHeader(model: controller.userspace!),
                        XinDongKongJianInfo(model: controller.userspace!),
                        const XinDongKongJianInfoMoment(),
                        XinDongKongJianInfoPhoto(model: controller.userspace!),
                        const XinDongKongJianInfoAuth(),
                        XinDongKongJianInfoBasic(model: controller.userspace!),
                        XinDongKongJianInfoSlogan(model: controller.userspace!),
                        XinDongKongJianInfoTags(model: controller.userspace!),
                        XinDongKongJianInfoSelfSlogan(model: controller.userspace!),
                        // XinDongKongJianInfoNoble(model: controller.userspace!),
                        XinDongKongJianInfoGift(model: controller.userspace!),
                      ]),
                ),
                // 导航栏
                const Positioned(top: 0, left: 0, right: 0, child: XinDongKongJianAppBar()),
                // 工具栏
                controller.userspace!.isSelf
                    ? const SizedBox()
                    : Positioned(
                        bottom: 0,
                        left: 0,
                        right: 0,
                        child: XinDongKongJianToolBar(model: controller.userspace!),
                      ),
              ],
            );
          }),
    );
  }
}
